package com.group2.view;
import static model.netBase.IP;
import javax.swing.JPanel;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Vector;

import model.*;
public class BankRemindPayFrame extends JPanel {
	private JTable paytable;
	private JComboBox comboBox=new JComboBox();
	private static ArrayList<Account>list=null;

	/**
	 * Create the panel.
	 */
	public BankRemindPayFrame() throws IOException, ClassNotFoundException {
		setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JLabel lblNewLabel = new JLabel("费用类型：");

		comboBox.setModel(new DefaultComboBoxModel(new String[] {"所有", "水电费", "住宿费", "学费"}));
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					paysearchActionPerformed(e);
				} catch (IOException ex) {
					throw new RuntimeException(ex);
				} catch (ClassNotFoundException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(BankRemindPayFrame.class.getResource("/images/search.png")));
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblNewLabel_1 = new JLabel("未缴费学生名单：");
		
		JButton btnNewButton_1 = new JButton("催缴费用");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RemindPayActionPerformed(e);
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap(31, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
							.addGap(45)
							.addComponent(btnNewButton)
							.addGap(72))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addGap(268))
								.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 364, GroupLayout.PREFERRED_SIZE))
							.addGap(25))))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(168)
					.addComponent(btnNewButton_1)
					.addContainerGap(172, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(30)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton))
					.addGap(29)
					.addComponent(lblNewLabel_1)
					.addGap(2)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(btnNewButton_1)
					.addContainerGap(21, Short.MAX_VALUE))
		);
		
		paytable = new JTable();
		paytable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u59D3\u540D", "\u5B66\u53F7", "\u91D1\u989D", "\u8D39\u7528\u7C7B\u578B", "\u7F34\u7EB3\u671F\u9650"
			}
		));
		scrollPane.setViewportView(paytable);
		setLayout(groupLayout);
		reFresh();

	}
	private void reFresh() throws IOException, ClassNotFoundException {
		String str= (String) comboBox.getSelectedItem();
		String type=null;
		Account account=new Account();
		if(str.equals("所有")) type="";
		else type=account.toType(str);
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("66" + '\n'+type+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		ObjectInputStream in = new ObjectInputStream(is);
		Object obj = in.readObject();
		list = (ArrayList<Account>) obj;
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
		fillbilltable(list);
	}


	/**
	 * 初始化表格数据
	 */
	private void fillbilltable(ArrayList<Account>course) {
		DefaultTableModel dtm=(DefaultTableModel) paytable.getModel();
		String str= (String) comboBox.getSelectedItem();
		System.out.println(str);
		dtm.setRowCount(0);//设为0行
		//连接数据库
		try {
			int size = course.size();
			int index = 0;
			while (size > 0) {
				Vector v = new Vector();
				if(course.get(index).getIFAccommodationFee()>0&&(str.equals("所有")||str.equals("住宿费"))) {
					v.add(course.get(index).getAccountName()) ;
					v.add(course.get(index).getAccountId());
					v.add(course.get(index).getIFAccommodationFee());
					v.add("住宿费");
					v.add(course.get(index).getAccomDeadline());
					dtm.addRow(v);
				}
				if(course.get(index).getIFTution()>0&&(str.equals("所有")||str.equals("学费"))) {
					v.add(course.get(index).getAccountName()) ;
					v.add(course.get(index).getAccountId());
					v.add(course.get(index).getIFTution());
					v.add("学费");
					v.add(course.get(index).getTutionDeadline());
					dtm.addRow(v);
				}
				if(course.get(index).getIFUtilityBill()>0&&(str.equals("所有")||str.equals("水电费"))) {
					v.add(course.get(index).getAccountName()) ;
					v.add(course.get(index).getAccountId());
					v.add(course.get(index).getIFUtilityBill());
					v.add("水电费");
					v.add(course.get(index).getUtilityDeadline());
					dtm.addRow(v);
				}
				size--;
				index++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 费用查询事件处理
	 *
	 */
	private void paysearchActionPerformed(ActionEvent e) throws IOException, ClassNotFoundException {
		comboBox.getSelectedIndex();//0所有 1水电费 2住宿费 3学费
		//连接数据库
		reFresh();
	}
	/**
	 * 催缴费用事件处理
	 * @param e
	 */
	private void RemindPayActionPerformed(ActionEvent e) {
		
	}
}
